#include "BlockQueue.hpp"
#include "Task.hpp"
#include <unistd.h>
#include <vector>

const int CON_NUM = 1, PRO_NUM = 1;

void* con_func(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*> (args);
    //BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);
    while(1)
    {
        int data = bq->Pop();
        std::cout << "consum data: " << data << std::endl;
        sleep(1);

        // task_t t = bq->Pop();
        // t();
        // sleep(1);
    }
}

void* pro_func(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*> (args);
    //BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*> (args);
    
    while(1)
    {
        sleep(1);
        int data = rand() % 1000;
        std::cout << "produce a data: " << data << std::endl;
        bq->Equeue(data);
        // std::cout << "生产了一个任务: " << std::endl;
        // bq->Equeue(Download);
        // sleep(1);
    }
}


int main()
{
    srand((unsigned int)time(0));

    std::vector<pthread_t> con, pro;
    BlockQueue<int>* bq = new BlockQueue<int>();
    //BlockQueue<task_t> *bq = new BlockQueue<task_t>();

    for(int i = 0; i < CON_NUM; i++)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, con_func, bq);
        con.push_back(tid);
    }

    
    for(int i = 0; i < PRO_NUM; i++)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, pro_func, bq);
        pro.push_back(tid);
    }


    for(auto& tid : con) pthread_join(tid, nullptr);
    for(auto& tid : pro) pthread_join(tid, nullptr);

    return 0;
}